//
//  YZMImageActionView.m
//  YZMicroStock
//
//  Created by apple on 17/1/4.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMImageActionView.h"
#import "YZMImageActionTableViewCell.h"

@interface YZMImageActionView ()

@end

@implementation YZMImageActionView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self) {
        self=[super initWithFrame:frame];
        [self createObject];
    }
    return self;
}

- (void)showInView: (UIView *)view{
    
    self.alpha=0;
    [view addSubview: self];
    self.frame = view.bounds;
    [UIView animateWithDuration: .4 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)show{
    
    UIView *view = [[YZMControllerManager sharedManager] currentNavationCtroller].view;
    [self showInView: view];
}


- (void)dismissWithAnimationFlag:(BOOL)animation{
    
    if (animation) {
        
        self.alpha = 1;
        [UIView animateWithDuration: .4f animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }else{
        
        [self removeFromSuperview];
    }
}

- (void)setDatasource:(NSMutableArray *)datasource {
    
    _datasource = datasource;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.width.equalTo(self);
        make.height.mas_equalTo(_datasource.count*60);
    }];
}

-(void)createObject{
    
    self.backgroundColor = [UIColor colorWithRed: 0 green:0 blue:0 alpha:0.5];
    self.tableView=[[UITableView alloc]init];
    [self addSubview:self.tableView];
    self.tableView.scrollEnabled=NO;
    [self.tableView registerClass:[YZMImageActionTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _datasource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YZMImageActionTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleString=_datasource[indexPath.row];
    
    YZMIndexCount indexCount;
    
    indexCount.index = indexPath.row;
    indexCount.count = _datasource.count;
    cell.indexCount = indexCount;
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_datasource.count==3) {
        switch (indexPath.row) {
                
            case 0:
                [self dismissWithAnimationFlag:YES];
                if (self.shareBlock) {
                    self.shareBlock();
                }
                break;
                
            case 1:
                [self dismissWithAnimationFlag:YES];
                if (self.saveBlock) {
                    self.saveBlock();
                }
                break;
                
            case 2:
                [self dismissWithAnimationFlag:YES];
                break;
                
            default:
                break;
        }
    }else{
        switch (indexPath.row) {
                
            case 0:
                [self dismissWithAnimationFlag:YES];
                if (self.saveBlock) {
                    self.saveBlock();
                }
                break;
                
            case 1:
                [self dismissWithAnimationFlag:YES];
                break;
                
            default:
                break;
        }
    }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//- (void)show
//{
//    UIView *view = [[YZMControllerManager sharedManager] currentNavationCtroller].view;
//    [self showInView: view];
//}

@end
